﻿using System;
using System.Data.SqlClient;
using System.Collections.Generic;

namespace DataCoreTechnologies.SqlServer
{
   /// <summary>
   /// Categorized functions into types.
   /// </summary>
   public enum FunctionType
   {
      Undefined,
      TableFunction,
      ScalarFunction,
      MultiStatement
   }

   /// <summary>
   /// Contains information about a function belonging to a schema.
   /// </summary>
   public sealed class Function : DatabaseObject
   {
      #region Public
      //Properties
      /// <summary>
      /// The T-Sql statement used to create this function.
      /// </summary>
      public string DDL { get; internal set; }
      /// <summary>
      /// The category that this function belongs to.
      /// </summary>
      public FunctionType FunctionType { get; internal set; }
      /// <summary>
      /// A collection of parameters belonging to this function.
      /// </summary>
      public IEnumerable<Parameter> Parameters
      {
         get
         {
             for (int index = 0; index < _parameters.Count; index++)
             {
                 yield return _parameters[index];
             }
         }
      }
      /// <summary>
      /// A collection of lines of code containing the T-Sql create statement for this function.
      /// </summary>
      public IEnumerable<string> CodeLines
      {
         get
         {
             for (int index = 0; index < _codeLines.Count; index++)
             {
                 yield return _codeLines[index];
             }
         }
      }
      /// <summary>
      /// A collection of extended properties associated with this function.
      /// </summary>
      public IEnumerable<ExtendedProperty> ExtendedProperties
      {
         get
         {
             for (int index = 0; index < _extendedProperties.Count; index++)
             {
                 yield return _extendedProperties[index];
             }
         }
      }
      //Methods
      /// <summary>
      /// Returns a collection of T-Sql keywords comprimising the create statement for this function.
      /// </summary>
      /// <returns>
      /// The collection of T-Sql keywords comprimising the create statement for this function.
      /// </returns>
      public List<string> GetDdlTokens()
      {
         return TSql.GetDDLTokens(DDL);
      }
      #endregion Public
      #region Internal
      //Constructor
      /// <summary>
      /// Creates a function and associates it with the schema specified by the parameter.
      /// </summary>
      /// <param name="container">The schema this function belongs to.</param>
      internal Function(Schema container)
      {
         base.Schema = container;
         base.InstanceId = System.Guid.NewGuid();
      }
      //Methods
      /// <summary>
      /// Associates a parameter with this function.
      /// </summary>
      /// <param name="parameter">The parameter that belongs to this function.</param>
      internal void AddParameter(Parameter parameter) { _parameters.Add(parameter); }
      /// <summary>
      /// Associates a line of code with this function.
      /// </summary>
      /// <param name="codeLine">The line of code that belongs to this function.</param>
      internal void AddCodeLine(string codeLine) { _codeLines.Add(codeLine); }
      /// <summary>
      /// Associates a extended property with this function.
      /// </summary>
      /// <param name="extendedProperty">The extended property that belongs to this function.</param>
      internal void AddExtendedProperty(ExtendedProperty extendedProperty) { _extendedProperties.Add(extendedProperty); }
      #endregion Internal
      #region Private
      //Properties
      private List<Parameter> _parameters = new List<Parameter>();
      private List<string> _codeLines = new List<string>();
      private List<ExtendedProperty> _extendedProperties = new List<ExtendedProperty>();
      #endregion Private
   }
}
